package com.aaa.backsystem.controller;


import com.aaa.backsystem.entity.PetrolStationType;
import com.aaa.backsystem.entity.Result;
import com.aaa.backsystem.entity.User;
import com.aaa.backsystem.service.PetrolStationTypeService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author gyc
 * @since 2020-07-27
 */
@Controller
@RequestMapping("/petrolStationType")
public class PetrolStationTypeController extends BaseController {

    @Autowired
    private PetrolStationTypeService petrolStationTypeService;

    /**
     * 设置每种类型加油站的阈值
     *
     * @param minPSThreshold
     * @param middlePSThreshold
     * @param maxPSThreshold
     * @return
     */
    @ResponseBody
    @RequestMapping("setThreshold")
    public Result setThreshold(Integer minPSThreshold, Integer middlePSThreshold, Integer maxPSThreshold) {
        PetrolStationType petrolStationType1 = new PetrolStationType(1, minPSThreshold);
        boolean i1 = petrolStationTypeService.updateById(petrolStationType1);
        PetrolStationType petrolStationType2 = new PetrolStationType(2, middlePSThreshold);
        boolean i2 = petrolStationTypeService.updateById(petrolStationType2);
        PetrolStationType petrolStationType3 = new PetrolStationType(3, maxPSThreshold);
        boolean i3 = petrolStationTypeService.updateById(petrolStationType3);
        if (i1 && i2 && i3) {
            return success();
        } else {
            return error();
        }
    }

    @RequestMapping("/warningValue")
    @ResponseBody
    public Result warningValue() {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
       if(user.getPetrolStationId()==null){
           user.setUserId(null);
       }
        try {
            PetrolStationType petrolStationType = petrolStationTypeService.warningValue(user.getUserId());
            return success(petrolStationType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return error();
    }

}

